package cn.iocoder.yudao.module.promotion.controller.app.yearcardconfig;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.promotion.controller.app.yearcardconfig.vo.AppYearCardConfigRespVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.yearcardconfig.YearCardConfigDO;
import cn.iocoder.yudao.module.promotion.service.yearcardconfig.YearCardConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 年卡配置")
@RestController
@RequestMapping("/promotion/year-card-config")
@Validated
public class AppYearCardConfigController {

    @Resource
    private YearCardConfigService yearCardConfigService;

    @GetMapping("/get")
    @Operation(summary = "获得年卡配置")
    public CommonResult<AppYearCardConfigRespVO> getYearCardConfig() {
        YearCardConfigDO yearCardConfig = yearCardConfigService.getYearCardConfig();
        if (yearCardConfig != null) {
            if (Objects.equals(2, yearCardConfig.getPutType())
                    && (LocalDateTime.now().isBefore(yearCardConfig.getPutTimeStart()) || LocalDateTime.now().isAfter(yearCardConfig.getPutTimeEnd()))) {
                yearCardConfig = null;
            }
        }
        return success(BeanUtils.toBean(yearCardConfig, AppYearCardConfigRespVO.class));
    }

}